﻿/// displayStatusInfo

$.displayStatusInfo = function (statusInfo) {

    var statusNameTable = {
        'Error': 'error',
        'Success': 'succ',
        'Warning': 'warning'
    };

    var removeStatusInfoPanel = function () {
        // 如果已经有StatusPanel显示，那么删之
        if ($('#status-panel')) {
            $('#status-panel').remove();
        }
    };

    var getStatusInfoPanel = function (statusName, statusContent) {

        removeStatusInfoPanel();

        var statusPanelTemplate = '<div id="status-panel" class="status-{statusName}" style="display: block;">' +
                   				    '<span>{statusContent}</span>' +
       			                    '<div class="status-panel-funcs"><a id="btnHidePanel">关闭</a></div>' +
            				      '</div>';
        var statusPanelHtml = statusPanelTemplate
								.replace('{statusName}', statusName)
								.replace('{statusContent}', statusContent);


        var statusPanelDom = $(statusPanelHtml).prependTo('#content-page');
        statusPanelDom.find("#btnHidePanel")
			.click(removeStatusInfoPanel);

        return statusPanelDom;
    }

    var content = statusInfo.Info;
    var status = statusNameTable[statusInfo.Status];

    /// main
    var newPanel = getStatusInfoPanel(status, content);



    // 不自动
    // setTimeout(function () { element.fadeOut(); }, 3000);
}


$.displaySnapStatusInfo = function (statusInfo, autoHide) {

    if (typeof statusInfo == 'string' && statusInfo == 'close') {
        $('.snap-status-info').remove();
    } else {
        if (autoHide == null) {
            autoHide = true;
        }

        var status = statusInfo.Status;
        var info = statusInfo.Info;

        var panelDom = $('<div class="snap-status-info">')
        .addClass(status)
        .hide()
        .append(info)
        .appendTo('body')
        .fadeIn();

        if (autoHide) {
            setTimeout(function () {
                panelDom.fadeOut();
            }, 2000);
        }
    }
}

$.redirectAndDisplayInfo = function (targetUrl, statusInfo) {
    var formHtml = '<form id ="RedirectAndDisplayInfo" action="{targetUrl}" method=post>' +
                      '<input type="hidden" name="status" value="{statusValue}"/>' +
                      '<input type="hidden" name="info" value="{infoValue}"/>'
    '</form>';

    formHtml = formHtml.replace("{targetUrl}", targetUrl)
                       .replace("{statusValue}", statusInfo.Status)
                       .replace("{infoValue}", statusInfo.Info)

    $(formHtml)
      .appendTo('body')
      .submit();
}




